<?php
require('config.php');

if(!isset($_SESSION['id_user']))
    redirectFront('connexion');

$page = new rootLayoutPage();
$page->setBaliseTitle('profil_email_balise');
$page->setDescription('profil_email_description');
$page->addJavaScript('jquery.validate.js');

//-- menu Left show products--------------------------------//
$menu = new layoutMenuLeft();
$menuCategorie = new layoutMenuPage();
$menu->pushMenu($menuCategorie);
$menuPage = new layoutMenuPage();
$menu->pushMenu($menuPage);
$page->setMenuPage($menu);

$translate = tmxTranslate('profil-email.xml', REP_XLAT_TEMPLATE);
$subject_mail = tmxTranslate('subject.mail.xml', REP_XLAT_MAIL);
/** traitement *********************************/
if(isset($_POST['submit'])) {
    try {
        $new_email = trim($_POST['email']);
       // if(!isValidEmail($new_email))
       //     throw new Exception($translate['message_1']);

        $user = modelUser::getUserMail($new_email);
        if($user)
            throw new Exception($translate['message_1']);

		modelUser::changeKeyUnique();
        $profil = modelUser::getProfil($_SESSION['id_user']);
        // mail nouvelle adresse
    	$mail = new flyLayout(REP_MAIL.'modification-email-newadr.mail.tpl');
    	$mail->start();
    	$language_folder = $_SESSION['language_code']==language_default?'':$_SESSION['language_code'].'/';
        $mail->setVariable('lien_activation', HTTP_HOST.$language_folder.'validation-email.php?id='.$profil->id_user.'&clef='.$profil->key_unique);
    	$mail->setVariable('email', $new_email);
        $mail->stop();
    	$subject = $subject_mail['profil_email_1'].' '.MAIL_SITENAME;

    	if(STATUS_EMAIL == 0) {
    		echo $mail;
    	}elseif(STATUS_EMAIL == 1) {
    		monsterMail($new_email, $subject, $mail);
    	}else {
    		echo $mail;
    		monsterMail($new_email, $subject, $mail);
    	}

    	// mail ancienne adresse
    	$mail = new flyLayout(REP_MAIL.'modification-email-oldadr.mail.tpl');
    	$mail->start();
        $mail->setVariable('email', $new_email);
    	$mail->stop();
    	$subject = $subject_mail['profil_email_2'].' '.MAIL_SITENAME;

    	if(STATUS_EMAIL == 0) {
    		echo $mail;
    	}elseif(STATUS_EMAIL == 1) {
    		monsterMail($profil->login, $subject, $mail);
    	}else {
    		echo $mail;
    		monsterMail($profil->login, $subject, $mail);
    	}

    	// mise a jour
    	modelUser::setLoginModif($_SESSION['id_user'], $new_email);
        Persistence::register(PERSIST_MESSAGE, new Message($translate['message_2'], Message::INFO));
    	if(STATUS_EMAIL == 1) redirectFront('profil');
    }
    catch(Exception $e) {
        Persistence::register(PERSIST_MESSAGE, new Message($e->getMessage(), Message::AVERT));
    }
}

/** corps de la page ****************************/
$tpl_file='profil-email.tpl';
$profil_email = new flyLayout(REP_TPL . $tpl_file);
$profil_email->start();

//$tabs = new layoutTabs();
//$tabs->pushLink('Mon compte', '/profil.php');
//$tabs->pushLink('Mes messages', '/profil-message.php');
//$profil_email->includeLayout('tabs', $tabs);
	//$profil_email->setVariable('rep_ajax', REP_AJAX);

$profil_email->stop();
$page->setBody($profil_email);


$page->display();
?>